﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JukeBox.Properties;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;

namespace JukeBox
{
    static class ColumnsCollection
    {

        static ListColumn[] _DefaultM = {   new ListColumn("FileName","File Name"),
                                            new ListColumn("TrackNumber", "Track Number"),
                                            new ListColumn("Title", "Title"),
                                            new ListColumn("Artist", "Artists"),
                                            new ListColumn("Album", "Album"),
                                            new ListColumn("ID3v1","ID3v1"),
                                            new ListColumn("ID3v2", "ID3v2")};

        static string FilePath
        {
            get
            {
                string FileName = Resources.MCFileName;
                return Path.Combine(Application.UserAppDataPath, FileName);
            }
        }

        static List<ListColumn> _Columns;
        public static List<ListColumn> Columns
        {
            get
            {
                if (_Columns == null)
                {
                    _Columns = new List<ListColumn>();
                    Load();
                }
                return _Columns;
            }
        }

        public static void Load()
        {
            string FileName = Resources.MCFileName;
            FileName = Path.Combine(Application.UserAppDataPath, FileName);

            Columns.Clear();

            XmlSerializer S = new XmlSerializer(typeof(ListColumn[]));
            if (!File.Exists(FileName))
            {
                _Columns.AddRange(_DefaultM);
                return;
            }

            FileStream FS = new FileStream(FileName, FileMode.Open);
            Columns.AddRange((ListColumn[])S.Deserialize(FS));

            FS.Close();
        }

        public static void Save()
        {
            XmlSerializer S = new XmlSerializer(typeof(ListColumn[]));
            FileStream File = new FileStream(FilePath, FileMode.Create);
            S.Serialize(File, Columns.ToArray());
            File.Close();
        }

        public static ListColumn[] Default
        {
            get
            {
                return _DefaultM;
            }
        }
    }
}
